package com.system.struts2.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.base.Pagination.Pagination;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Dept;
import com.system.pojo.Group;
import com.system.pojo.Person;
import com.system.pojo.Position;
import com.system.service.IDeptService;
import com.system.service.IGroupService;
import com.system.service.IPersonService;
import com.system.service.IPositionService;
import com.system.struts2.util.MD5Util;
import com.system.struts2.util.PersonInfo;
import com.system.struts2.util.PersonUtil;

public class PersonAction extends ActionSupport {
	private String title;
	private String login;
	private Person person;
	private Dept dept;
	private Position position;
	private Group group;
	private IPersonService<Person> personSer;
	private IDeptService<Dept> deptSer;
	private IPositionService<Position> positionSer;
	private IGroupService<Group> groupSer;
	public List<Person> personList = new ArrayList<Person>();
	public List<Dept> deptList = new ArrayList<Dept>();
	public List<Position> positionList = new ArrayList<Position>();
	public List<Group> groupList = new ArrayList<Group>();
	private boolean flag = false;

	// 获取request、response
	HttpServletRequest request = null;
	HttpServletResponse response = null;// ServletActionContext.getResponse();
	private Pagination pagination;

	public String list() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();

		int totalCount = personSer
				.getTotalCount(" select count(p) from Person p "
						+ " where p.deleted = false ", null);

		pagination = new Pagination(request, response);
		pagination.setRecordCount(totalCount);

		personList = personSer.list(
				"from Person as p where p.deleted = false ", pagination.getFirstResult(),
				pagination.getPageSize(), null);
		//员工权限分配页面跳转
		if(this.login!=null&&this.login.equals("assign")){
			groupList = groupSer.list("from Group");
			return "assign";}
		return "list";
	}
	/*
	 * 员工权限分配实现
	 */
	public String assigned()
	{
		request = ServletActionContext.getRequest();
		String[] checkbox= request.getParameterValues("checkbox");
		return null;
	}
	public String initAdd() {

		deptList = deptSer.list("from Dept");
		positionList = positionSer.list("from Position ");
		groupList = groupSer.list("from Group");
		if (title != null && title.equals("modify")) {
			person = (Person) personSer.find(Person.class, person.getId());
			dept = person.getDept();
			position = person.getPosition();
			group = person.getGroup();
			return "modify";
		} else if(title != null &&title.equals("jingyanku"))
			return "jingyanu";
		  else

			return "initAdd";

	}

	public String add() {
		request = ServletActionContext.getRequest();

		deptList = deptSer.list("from Dept");
		positionList = positionSer.list("from Position ");
		groupList = groupSer.list("from Group");

		person.setIpCreated(request.getRemoteAddr());
		person.setIpLastActived(request.getRemoteAddr());
		person.setDateCreated(new Date());
		person.setDateLastActived(new Date());
		person.setDept(dept);
		person.setPosition(position);
		person.setGroup(group);

		flag = personSer.create(person);
		if (flag = false) {
			addActionMessage("帐号 " + person.getAccount() + " 已经存在。");
		}
		if (login!=null&&this.login.equals("worklog"))
			return "worklog";
		else if (login!=null&&this.login.equals("jingyanku"))
			return "jingyanku";
		else if (login!=null&&this.login.equals("industry"))
			return "industry";
		else if (login!=null&&this.login.equals("system"))
			return "system";
		else if(login!=null&&this.login.equals("index"))
			return "index";
		addActionMessage("注册用户成功！");
		return "success";
	}

	public String view() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		String id = request.getParameter("personId");
		if (id == null){
			if (person == null) {
				PersonInfo personInfo = PersonUtil.getPersonInfo(request,
						response);
				person = (Person) personSer.find(Person.class, personInfo
						.getId());
			}
		id = person.getId().toString();}
		person = (Person) personSer.find(Person.class, Integer.valueOf(id));
		return "personView";
	}
	public String road() {
		request = ServletActionContext.getRequest();
				response = ServletActionContext.getResponse();
				PersonInfo personInfo = PersonUtil.getPersonInfo(request,
						response);
				person = (Person) personSer.find(Person.class, personInfo
						.getId());
		return "modify";
	}

	public String modify() throws Exception {
		Person personQ = (Person) personSer.find(Person.class, person.getId());
		// BeanUtils.copyProperties(personQ,person);
		personQ.setAccount(person.getAccount());
		personQ.setName(person.getName());
		personQ.setDateLastActived(new Date());
		personQ.setDept(dept);
		personQ.setSex(person.getSex());
		personQ.setBirthday(person.getBirthday());
		personQ.setEmail(person.getEmail());
		personQ.setPosition(position);
		personSer.save(personQ);
		addActionMessage("修改成功");
		return "success";
	}

	public String pass() {
		Person personQ;
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		if (request.getParameter("personId") == null) {
			PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
			personQ = (Person) personSer.find(Person.class, personInfo.getId());
		} else
			personQ = (Person) personSer.find(Person.class, Integer
					.valueOf(request.getParameter("personId")));
		personQ.setPassword(MD5Util.calc(person.getPassword()));
		personSer.save(personQ);
		addActionMessage("密码修改成功！");
		return "success";

	}

	public String delete() {
		person = (Person) personSer.find(Person.class, person.getId());
		person.setDeleted(true);
		personSer.save(person);
		return list();
	}

	public String login() throws Exception {
		
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		person = personSer.getPerson(person.getAccount(), person.getPassword());
	
		if (person == null) {
			addActionMessage("用户名密码错误");
			if (this.login.equals("worklog"))
				return "work_logout";
			else if (this.login.equals("jingyanku"))
				return "jingyanku";
			else if (this.login.equals("industry"))
				return "industry_logout";
			else if (this.login.equals("system"))
				return "system_logout";
			else if(this.login.equals("index"))
				return "index";
		}
		
		PersonUtil.setPersonInf(request, response, person);

		person.setIpLastActived(request.getRemoteAddr());
		person.setDateLastActived(new Date());
		personSer.save(person);
		
			if (this.login.equals("worklog"))
				return "worklog";
			else if (this.login.equals("jingyanku"))
				return "jingyanku";
			else if (this.login.equals("industry"))
				return "industry";
			else if (this.login.equals("system"))
			{
				if(person.getGroup().getGroup().equals("admin"))
					return "admin";
				else
				return "system";
			}
			else if(this.login.equals("index"))
				return "index";
			return null;
	}

	public String logout() {
		request = ServletActionContext.getRequest();
		request.getSession(true).setAttribute(PersonUtil.PERSON_INFO, null);
		if (this.login.equals("worklog"))
			return "work_logout";
		else if (this.login.equals("jingyanku"))
			return "jingyanku";
		else if (this.login.equals("industry"))
			return "industry_logout";
		else
			return null;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}


	public IPersonService<Person> getPersonSer() {
		return personSer;
	}

	public void setPersonSer(IPersonService<Person> personSer) {
		this.personSer = personSer;
	}

	public IDeptService<Dept> getDeptSer() {
		return deptSer;
	}

	public void setDeptSer(IDeptService<Dept> deptSer) {
		this.deptSer = deptSer;
	}

	public IPositionService<Position> getPositionSer() {
		return positionSer;
	}

	public void setPositionSer(IPositionService<Position> positionSer) {
		this.positionSer = positionSer;
	}

	public IGroupService<Group> getGroupSer() {
		return groupSer;
	}

	public void setGroupSer(IGroupService<Group> groupSer) {
		this.groupSer = groupSer;
	}

	public List<Person> getPersonList() {
		return personList;
	}

	public void setPersonList(List<Person> personList) {
		this.personList = personList;
	}

	public List<Dept> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

	public List<Position> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<Position> positionList) {
		this.positionList = positionList;
	}

	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
}
